package com.lht.leetcodeDemo;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-06-06
 */
public class RangeSumQueryImmutable {

    @Test
    public void test(){
        int[] arr=new int[]{-2,0,3,-5,2,-1};
        NumArray num=new NumArray(arr);

        System.out.println(num.sumRange(0,2));
    }

}

class NumArray {
    private int[] nums;
    private int[] sums;
    public NumArray(int[] nums) {
        this.nums=nums;
        this.sums=new int[this.nums.length+1];
        this.sums[0]=0;
        for(int i=0;i<nums.length;i++){
            this.sums[i+1]=this.sums[i]+this.nums[i];
        }
    }

    public int sumRange(int i, int j) {
        int start=Math.max(0,i);
        int end=Math.min(j,this.nums.length-1);
        return this.sums[end+1]-this.sums[start];
    }

}
